/*
 *  Copyright 2009 Advanced Industrial Science and Technology (AIST).
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/*! \file
 *  \brief
 */
#pragma once

// boost, includes
//#include <boost/type_traits/same_traits.hpp> // for is_same
#include <thrust/detail/type_traits.h>

// thrust, includes
#include <thrust/pair.h>

namespace thrust {
  
  namespace detail {
    
    struct error_property_not_found { };
    
    template <class PropertyTag1, class PropertyTag2>
    struct same_property {
      //enum { value =boost::is_same<PropertyTag1,PropertyTag2>::value };
      enum { value =is_same<PropertyTag1,PropertyTag2>::value };
    };
    
    
    template <int TagMatched>
    struct property_value_dispatch {
      // static method
      template <class PropertyTag, class T, class Tag>
      __host__ __device__
      inline static T& 
      get_value( PropertyTag& p, T*, Tag) {
        return p.m_value;
      }
      template <class PropertyTag, class T, class Tag>
      __host__ __device__
      inline static const T& 
      const_get_value( const PropertyTag& p, T*, Tag) {
        return p.m_value;
      }
    };
    
    template <class PropertyList>
    struct property_value_end {
      
      template <class T>
      struct result { typedef T type; };
      
      // static method
      template <class T, class Tag>
      __host__ __device__
      inline static T& 
      get_value( PropertyList& p, T* t, Tag tag) {
        typedef typename PropertyList::next_type Next;
        typedef typename Next::tag_type          Next_tag;
        
        enum { match =same_property<Next_tag,Tag>::value };
        
        Next& next_p =static_cast<Next&>(p);	
        return property_value_dispatch<match>::get_value( next_p, t, tag);
      }
      
      template <class T, class Tag>
      __host__ __device__
      inline static const T& 
      const_get_value( const PropertyList& p, T* t, Tag tag) {
        typedef typename PropertyList::next_type Next;
        typedef typename Next::tage_type         Next_tag;
        
        enum { match =same_property<Next_tag,Tag>::value };
        
        Next& next_p =static_cast<Next&>(p);
        return property_value_dispatch<match>::const_get_value( next_p, t, tag);
      }      
    };
    
    template <>
    struct property_value_end<no_property> {
      
      template <class T>
      struct result {
        typedef detail::error_property_not_found type;
      };
      
      // Stop the recursion and return error
      // static method
      template <class T, class Tag>
      __host__ __device__
      inline static detail::error_property_not_found
      //inline static detail::error_property_not_found&
      get_value( no_property&, T*, Tag) {
        //const static error_property_not_found s_prop_not_found;
        error_property_not_found s_prop_not_found;
        return s_prop_not_found;
      }
      template <class T, class Tag>
      __host__ __device__
      inline static detail::error_property_not_found
      //inline static const detail::error_property_not_found&
      const_get_value( const no_property&, T*, Tag) {
        //static error_property_not_found s_prop_not_found;
        error_property_not_found s_prop_not_found;
        return s_prop_not_found;
      }
    };
    
    template <>
    struct property_value_dispatch<0> {
      template <class PropertyList, class T, class Tag>
      __host__ __device__
      inline static typename property_value_end<PropertyList>::template result<T>::type&
      get_value( PropertyList& p, T* t, Tag tag) {
        return property_value_end<PropertyList>::get_value( p, t, tag);
      }
      template <class PropertyList, class T, class Tag>
      __host__ __device__
      inline static const typename property_value_end<PropertyList>::template result<T>::type&
      const_get_value( const PropertyList& p, T* t, Tag tag) {
        return property_value_end<PropertyList>::const_get_value( p, t, tag);
      }
    };
    
    //================================================================================
    // build_proeprty_tag_value_alist method expand property into 
    // pair<pair<tag1,value_type1>,Next>>, Next is pair<pair<tag2,value_type2>,Next>, 
    // recursively. last pair is pair<pair<tag,value_type>,no_property>
    template <class PropertyList>
    struct build_property_tag_value_alist
    {
      typedef typename PropertyList::next_type NextProperty;
      typedef typename PropertyList::value_type Value;
      typedef typename PropertyList::tag_type Tag;
      typedef typename build_property_tag_value_alist<NextProperty>::type Next;
      typedef typename thrust::pair<thrust::pair<Tag,Value>,Next> type;
    };
    template <>
    struct build_property_tag_value_alist<no_property>
    {
      typedef no_property type;
    };
    
    template <class TagValueAlist, class Tag>
    struct extract_value {
      typedef error_property_not_found type;
    };
    template <class Value, class Tag1, class Tag2, class Rest>
    struct extract_value<thrust::pair<thrust::pair<Tag1,Value>,Rest>,Tag2> {
      typedef typename extract_value<Rest,Tag2>::type type;
    };
    template <class Value, class Tag, class Rest>
    struct extract_value<thrust::pair<thrust::pair<Tag,Value>,Rest>,Tag> {
      typedef Value type;
    };
    
  } // namespace detail

} // namespace thrust
